package top.ljlgithub.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.ljlgithub.api.bean.*;
import top.ljlgithub.api.common.response.Meta;
import top.ljlgithub.api.common.response.ResponseEntity;
import top.ljlgithub.api.exception.ParamException;
import top.ljlgithub.api.mapper.GoodsMapper;
import top.ljlgithub.api.service.GoodsService;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/5/7 15:29
 * @Version 1.0
 **/
@RestController
@CrossOrigin
public class GoodsController {
    @Autowired
    GoodsService goodsService;
    @GetMapping("/goods")
    public ResponseEntity getGoodsList(PageCondition pageCondition) throws ParamException {
        String query = pageCondition.getQuery();
        Integer pagesize = pageCondition.getPageSize();
        Integer pagenum = pageCondition.getPageNum();
        if (pagesize == null || pagenum == null){
            throw new ParamException();
        }
        GoodsPageBean goodsList = goodsService.getGoodsList(new GoodsParam(query, pagenum, pagesize));
        Meta meta = new Meta("获取成功",200);
        ResponseEntity<Object> result = new ResponseEntity<>(goodsList,meta);
        return result;
    }

    @PostMapping("/goods")
    public ResponseEntity addGoods(@RequestBody AddGoods addGoods){
        GoodsInfo goodsInfo = goodsService.addGoods(addGoods);
        Meta meta = new Meta("创建商品成功",201);
        ResponseEntity<Object> result = new ResponseEntity<>(goodsInfo,meta);
        return result;
    }

    @DeleteMapping("/goods/{id}")
    public ResponseEntity deleteGoods(@PathVariable("id") Integer id){
        Meta meta = new Meta("删除成功",200);
        goodsService.deleteGoods(id);
        ResponseEntity<Object> result = new ResponseEntity<>(null,meta);
        return result;
    }

    @GetMapping("/goods/{id}")
    public ResponseEntity getGoodsInfo(@PathVariable("id") Integer id){
        GoodsInfo goodsInfo = goodsService.getGoodsInfo(id);
        Meta meta = new Meta("获取成功",200);
        ResponseEntity<Object> result = new ResponseEntity<>(goodsInfo,meta);
        return result;
    }

    @PutMapping("/goods/{id}")
    public ResponseEntity updateGoods(@PathVariable("id") Integer id,@RequestBody AddGoods addGoods) throws ParamException {
        addGoods.setGoods_id(id);
        String goods_name = addGoods.getGoods_name();
        if (goods_name == null || goods_name.equals("")){
            throw new ParamException();
        }
        Integer goods_price = addGoods.getGoods_price();
        if (goods_price == null || goods_price.equals("")){
            throw new ParamException();
        }
        Integer goods_number = addGoods.getGoods_number();
        if (goods_number == null || goods_number.equals("")){
            throw new ParamException();
        }
        Integer goods_weight = addGoods.getGoods_weight();
        if (goods_weight == null || goods_weight.equals("")){
            throw new ParamException();
        }
        GoodsInfo goodsInfo = goodsService.updateGoods(addGoods);
        Meta meta = new Meta("更新成功", 200);
        ResponseEntity<Object> result = new ResponseEntity<>(goodsInfo,meta);
        return result;
    }
}
